<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <h5>this is index.html</h5>
  <iframe id="testIframe" src="http://localhost:4000/postmessage/index1.html" frameborder="0" onload="load()"></iframe>
  <button onclick="changeStyle()">修改颜色</button>
  <script>
    function load() {
      const iframe = document.getElementById('testIframe');
      iframe.contentWindow.postMessage('我爱你', 'http://localhost:4000')
    }

    function changeStyle() {
      var x=document.getElementById("testIframe");
      var y=(x.contentWindow || x.contentDocument);
      if (y.document)y=y.document;
      y.body.style.backgroundColor="#0000ff"; // 不同域不能修改
    }

    window.onmessage = function(e) {
      console.log('index.html', e)
    }
  </script>
</body>
</html>